<template>
  <!-- <div class='container'>学科管理</div> -->

  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>
        学科名称
        <el-input
          v-model="input"
          placeholder="请输入内容"
          style="width: 180px"
        ></el-input>

        <el-button @click="clean" style="margin: 0px 0px 0px 5px">清除</el-button>
        <el-button type="primary" @click="screen">搜索</el-button>
      </span>

      <el-button
        style="float: right;"
        type="success"
        @click="addSubjects"
        round
        >新增学科</el-button
      >
      <subjectsAdd
      ref="refData"
        :dialogVisible.sync="dialogVisible"
        @adddata="adddata"
        :tableData="tableData"
      ></subjectsAdd>
      <div>
        <el-alert :title="title" class="alert" type="info"> </el-alert>
      </div>
    </div>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" :index="indexMethod" label="序号">
      </el-table-column>

      <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
      <el-table-column prop="username" label="创建者"> </el-table-column>
      <el-table-column prop="addDate" label="创建日期">
        <template #default="{row}">
          {{row.addDate|dataFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示">
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录">
      </el-table-column>
      <el-table-column prop="tags" label="标签"> </el-table-column>
      <el-table-column prop="totals" label="题目数量"> </el-table-column>
      <el-table-column prop="address" label="操作" >
        <template #default="{ row }">
          <el-button type="text" @click="$router.push({name:'subjects-directorys',query:{id:row.id,name:row.subjectName}})">学科分类</el-button>
          <el-button type="text" @click="$router.push({name:'subjects-tags',query:{id:row.id,name:row.subjectName}})">学科标签</el-button>
          <el-button type="text" @click="modification(row)">修改</el-button>
          <el-button type="text" @click="delSubject(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <!-- <span class="demonstration">完整功能分页</span> -->
      <el-pagination
        style="float: right;margin: 10px 0px 10px 0px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        background
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { list, detail, remove } from '@/api/hmmm/subjects.js'
import subjectsAdd from '../components/subjects-add.vue'
export default {
  components: {
    subjectsAdd
  },
  data () {
    return {
      dialogVisible: false,
      input: '',
      page: 1,
      pagesize: 10,
      tableData: [],
      counts: 0,
      title: ''

    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 学科列表
    async getList () {
      const res = {
        page: this.page,
        pagesize: this.pagesize
      }
      const { data } = await list(res)
      // console.log(data)
      this.tableData = data.items
      // console.log(this.tableData)
      this.counts = data.counts
      this.title = `数据一共${data.counts}条`
    },
    // 点击清空输入框
    clean () {
      this.input = ''
    },
    // 获取列表数据
    async screen () {
      // console.log(this.input)
      const obj = {
        page: 1,
        pagesize: 10,
        subjectName: this.input
      }
      const { data } = await list(obj)
      // console.log(data)
      this.tableData = data.items
      this.counts = data.counts
    },
    // 点击新增展示弹框
    addSubjects () {
      this.$refs.refData.FormData.subjectName = ''
      this.dialogVisible = true
    },
    // 添加之后关闭弹框刷新列表
    adddata () {
      this.dialogVisible = false
      this.getList()
    },
    // 点击修改
    async modification (id) {
      // console.log(id)
      // this.id = id
      this.dialogVisible = true
      const { data } = await detail(id)
      // console.log(data)
      //  data.subjectName
      // console.log(this.$refs.refData.FormData)
      // this.$refs.refData.FormData.subjectName = data.subjectName
      // this.$refs.refData.FormData.id = data.id
      this.$refs.refData.FormData = data
    },
    // 点击删除
    delSubject (id) {
    //  console.log();
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        await remove(id)

        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    indexMethod (index) {
      // console.log(index)
      return index + 1 + (this.page - 1) * this.pagesize
    },
    handleSizeChange (val) {
      // console.log(val)
      this.pagesize = val
      // console.log(this.pagesize)
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(val)
      this.page = val
      this.getList()
    }
  }
}
</script>

<style scoped lang='less'>
.alert {
  margin: 15px 0px;
}
</style>
